package org.hismig.report;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.hismig.migration.Context;
import org.hismig.migration.executor.DBExecutor;
import org.hismig.migration.result.JobResultInfo;
import org.hismig.scheduler.ITriggerListener;
import org.hismig.utils.DateTool;
import org.hismig.utils.FileTool;

/**
 * @declare: //TODO <br />
 *           This class created by Vansion.Hopihit on 2012-8-15 <br />
 *           <a href="mailto:vansionhopihit@gmail.com">Contact Me</a>
 * @version 1.0
 * @author Vansion.Hopihit
 * 
 */
public class JobResultReport implements ITriggerListener {

	public static final String REPORT_PATH = "ResultReport";

	public void outputReport(JobResultInfo jobResultInfo) {

		Velocity.init();

		VelocityContext context = new VelocityContext();

		context.put("job", jobResultInfo);

		// to formate the date
		context.put("dateTool", DateTool.class);

		OutputStreamWriter ow = null;

		try {
			ow = new OutputStreamWriter(new FileOutputStream(
					getReportFile(jobResultInfo)), "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}

		InputStreamReader r = null;
		try {
			r = new InputStreamReader(getClass().getResourceAsStream(
					"JobResultReport.vm"), "UTF-8");
		} catch (UnsupportedEncodingException e2) {
			e2.printStackTrace();
		}

		try {
			Velocity.evaluate(context, ow, "HisMig", r);
		} catch (ParseErrorException e) {
			e.printStackTrace();
		} catch (MethodInvocationException e) {
			e.printStackTrace();
		} catch (ResourceNotFoundException e) {
			e.printStackTrace();
		}

		try {
			ow.flush();
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		try {
			ow.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			r.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static File getReportFile(JobResultInfo jobResultInfo) {
		return FileTool.getRuntimeFile(
				getReportFileName(jobResultInfo),
				REPORT_PATH + File.separator
						+ DateTool.getNormalDate(jobResultInfo.getEndDate()));

	}

	public static String getReportFileName(JobResultInfo jobResultInfo) {
		return String.format(
				"HisMig-ResultReport-%s_%s.html",
				new Object[]{
						DateTool.getShortDate(jobResultInfo.getStartDate()),
						DateTool.getShortTime(jobResultInfo.getStartDate())});

	}

	@Override
	public void beforeTriggerStart(Context context, List<DBExecutor> dbExecutors) {

	}

	@Override
	public void afterTriggerEnd(JobResultInfo jobResultInfo) {
		outputReport(jobResultInfo);
	}

}
